var $grid = $('#bt_custom_ask_grid'), $khGrid = $('#bt_custom_inf_grid');
var viewDialog=$('#dd');

context.ready = function() {
    $grid.datagrid({
        fit: true,
        border: true,
        // width:'95%',
        // height:'500',
        nowrap: true,
        autoRowHeight: true,
        striped: true,
        collapsible:true,
        url: _ROOT_ + '/custom/getData',
        pagination: true,
        columns: [[
                {field:'ck',checkbox: true}, 
                {field: 'PO', title: 'PO', width: 100, align: 'center'},
                {field: '邮件时间', title: '邮件时间' ,align: 'center',width: 150},
                {field: '需求品种', title: '需求品种' ,align: 'center'},
                {field: '需求数量', title: '需求数量' ,align: 'center'},
                {field: 'saname', title: '客户简称' ,align: 'center',width: 150},
                {field: 'name', title: '客户信息' ,align: 'center',width: 150},
                {field: '开始时间', title: '开始时间' ,align: 'center'},
                {field: '截止时间', title: '截止时间' ,align: 'center'},
                {field: '实际发货数量', title: '实际发货数量' ,align: 'center'},
                {field: 'status',  title: '状态', width: 100, align: 'center', formatter: function(value) {
                        if (value === 'P') {
                            return '可用';
                        }
                        return '<font color="red">禁用</font>';
                }},
                {field: '备注', title: '备注', width: 200},
                {field: 'ID', title: '操作',  align: 'center', formatter: function(value) {
                                        return '<span title="编辑" class="img-btn icon-edit" ID=' + value + '></span>';
                }},
                {field: 'c_id', title: '客户信息ID', width: 100, align: 'center'}

            ]],
        toolbar: [{
                text: '新增',
                iconCls: 'icon-add',
                handler: context.addView
            }, {
                text: '删除',
                iconCls: 'icon-remove',
                handler: context.doDelete
            }],
        onLoadSuccess: function() {
            var $bodyView = $grid.data('datagrid').dc.view2;
            $bodyView.find('span[ID]').click(function(e) {
                e.stopPropagation();
                var uid = $(this).attr('ID');
                context.updateView(uid);
            });
        },
        onDblClickRow: function(rowIndex, rowData) {

            // if (c_id && c_id === rowData.c_id) {
            //     return;
            // }
            var $layout = $('#bt_custom_layout');
            var south = $layout.layout('panel', 'south');
            if (south.panel('options').collapsed) {
                $layout.layout('expand', 'south');
            }

            // $.alert(rowData.c_id);
            $khGrid.datagrid({
                url: _ROOT_ + '/custom/getinfData',
                queryParams: {c_id: rowData.c_id}
            });
        }
    });

    $khGrid.datagrid({
        fit: true,
        border: false,
        pagination: true,
        columns: [[
                {field:'ck',checkbox: true},
                {field:'saname', title: '公司简称'},
                {field:'name', title: '公司名称',width: 250},
                {field:'城市E', title: '城市E'},
                {field:'地址1', title: '地址1', width: 50},
                {field:'国家', title: '国家',},
                {field:'邮编', title: ''},
                {field:'FedEx帐号', title: 'FedEx帐号'},
                {field:'联系人', title: '联系人'},
                {field:'电话', title: '电话'}
            ]],
        toolbar: [{
                text: '新增',
                iconCls: 'icon-add',
                handler: context.addcfView
            }]
    });

    $('#bt_custom_search_btn').click(function() {
        var map = $('#bt_custom_search_from').toJson();
        for (var i = map.length - 1; i >= 0; i--) {
           alert(i.map[i]);
        };

        $grid.datagrid('load', $('#bt_custom_search_from').toJson());
    });
};

context.addView = function() {

    viewDialog.dialog({

        modal: true,  
        title: '新增订单',
        href: _ROOT_ + '/custom/toadd',
        width: 300,
        height: 400,    
        maximizable: true,
        resizable: true,
        bodyStyle: {overflow: 'hidden'},
        buttons: [{
                text: '提交',
                handler: context.doSubmit
            }]
    });
};

context.addcfView = function() {

    viewDialog.dialog({

        modal: true,  
        title: '新增订单',
        href: _ROOT_ + '/custom/tocfadd',
        width: 300,
        height: 400,    
        maximizable: true,
        resizable: true,
        bodyStyle: {overflow: 'hidden'},
        buttons: [{
                text: '提交',
                handler: context.doSubmit
            }]
    });
};


context.updateView = function(id) {
//  $.alert(id);
    viewDialog.dialog({
        title: '编辑出口清单',
        href: _ROOT_ + '/custom/toUpdate?ID=' + id,
        width: 400,
        height: 500,
        maximizable: true,
        resizable: true,
        bodyStyle: {overflow: 'hidden'},
        buttons: [{
                text: '提交',
                handler: context.doSubmit
            },]
    });
};

context.doDelete = function() {
    var checked = $grid.datagrid('getChecked');
    if (checked && checked.length > 0) {
        $.confirm('确认删除？', function(r) {
            if (r) {
                var ids = [];
                $.each(checked, function() {
                    ids.push(this.ID);
                });
                $.post(_ROOT_ + '/custom/doDelete', {ids: ids.join(',')}, function(rsp) {
                    if (rsp.status) {
                        $grid.datagrid('reload');
                    } else {
                        $.alert(rsp.msg);
                    }
                }, 'JSON');
            }
        });
    }
};

context.doSubmit = function() {
    $bt_custom_from = $('#bt_custom_from');
    // $.alert("sdfsdf");
    if ($bt_custom_from.form('validate')) {
        $.post(_ROOT_ + '/custom/doSave', $bt_custom_from.toJson(), function(rsp) {
            // $.alert(rsp.status);
            if (rsp.status) {
                $grid.datagrid('reload');
                
        viewDialog.dialog('close');
            } else {
                $.alert(rsp.msg);
            }
        }, "JSON");
    }
};
